package com.zte.claa.inficombo.csa.app.service.huaxiapr;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Service;

import com.zte.claa.inficombo.csa.app.service.huaxiapr.bean.HuaXiaRmRspGpioData;
import com.zte.claa.inficombo.csa.app.service.huaxiapr.bean.HuaXiaRmRspMsg;
import com.zte.claa.inficombo.csa.app.service.huaxiapr.bean.HuaXiaRmRspMsgCache;
import com.zte.claa.inficombo.csa.app.service.huaxiapr.bean.HuaXiaRmRspRs485Data;
import com.zte.claa.inficombo.csa.app.service.huaxiapr.bean.HuaXiaRmRspTrigerData;
import com.zte.claa.inficombo.csa.app.service.huaxiapr.bean.HuaXiaRmRspWhiteNameList;

/*
 * Author by 0283000106 
 */
@Service("HuaXia")
public class HuaXiaRmPlateRecogServiceImpl implements HuaXiaRmPlateRecogService, InitializingBean, DisposableBean {
	// 日志.
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	// 每个cam_id对应一个消息响应队列
	private Map<String, HuaXiaRmRspMsgCache> camidRspMsgMap = null; 
	
	@Override
	public void afterPropertiesSet() throws Exception {
		// 初始化图片数据缓存空间
		if(null == camidRspMsgMap){
			camidRspMsgMap = new HashMap<String, HuaXiaRmRspMsgCache>();
		}
	}
	       
	@Override
	public void destroy() throws Exception {
		try{
			if(this.camidRspMsgMap != null) {
				this.camidRspMsgMap.clear();
				this.camidRspMsgMap = null;
				logger.info("camidRspMsgMap release ok!");
			}
		}catch(Exception e){
			logger.error("destroy failed!", e);
		}
	}
	

	@Override
	public void cacheHuaXiaCmd_Gpio_Data(String camid, Map<String, Object> cmdParaMap) {
		if(null == camid || camid.equals("") || null == cmdParaMap || 0 == cmdParaMap.size()){
			logger.error("cacheHuaXiaCmd_Gpio_Data input para is null! camid="+camid+",cmdParaMap="+cmdParaMap);
			return;
		}
		HuaXiaRmRspMsgCache rspmsgcache = this.camidRspMsgMap.get(camid);
		if(null == rspmsgcache){
			rspmsgcache = new HuaXiaRmRspMsgCache();
			this.camidRspMsgMap.put(camid, rspmsgcache);
		}
		synchronized (rspmsgcache){  			
			HuaXiaRmRspGpioData gpio_data = rspmsgcache.getGpio_data();
			if(null == gpio_data){
				gpio_data = new HuaXiaRmRspGpioData();
				rspmsgcache.setGpio_data(gpio_data);
			}
			
			Map<String, String> eMap = new HashMap<>();
	        for (Map.Entry<String, Object> entry : cmdParaMap.entrySet()) {        	
	        	String fldname = entry.getKey();
	        	String fldval = (String)entry.getValue();
	        	eMap.put(fldname, fldval);
	        }	
	        if(eMap.size() > 0){
	        	gpio_data.addGpioDataElement(eMap);
	        	logger.info("cacheHuaXiaCmd_Gpio_Data add cache ok! gpio_data:"+gpio_data.toString());
	        }
		}
	}

	@Override
	public void cacheHuaXiaCmd_Rs485_Data(String camid, Map<String, Object> cmdParaMap) {
		if(null == camid || camid.equals("") || null == cmdParaMap || 0 == cmdParaMap.size()){
			logger.error("cacheHuaXiaCmd_Rs485_Data input para is null! camid="+camid+",cmdParaMap="+cmdParaMap);
			return;
		}
		HuaXiaRmRspMsgCache rspmsgcache = this.camidRspMsgMap.get(camid);
		if(null == rspmsgcache){
			rspmsgcache = new HuaXiaRmRspMsgCache();
			this.camidRspMsgMap.put(camid, rspmsgcache);
		}
		synchronized (rspmsgcache){
			HuaXiaRmRspRs485Data rs485_data = rspmsgcache.getRs485_data();
			if(null == rs485_data){
				rs485_data = new HuaXiaRmRspRs485Data();
				rspmsgcache.setRs485_data(rs485_data);
			}
			Map<String, String> eMap = new HashMap<>();
	        for (Map.Entry<String, Object> entry : cmdParaMap.entrySet()) {        	
	        	String fldname = entry.getKey();
	        	String fldval = (String)entry.getValue();
	        	eMap.put(fldname, fldval);
	        }
	        if(eMap.size() > 0){
	        	rs485_data.addRs485DataElement(eMap);
	        	logger.info("cacheHuaXiaCmd_Rs485_Data add cache ok! rs485_data:"+rs485_data.toString());
	        }
		}
	}

	@Override
	public void cacheHuaXiaCmd_Triger_Data(String camid, Map<String, Object> cmdParaMap) {
		if(null == camid || camid.equals("") || null == cmdParaMap || 0 == cmdParaMap.size()){
			logger.error("cacheHuaXiaCmd_Triger_Data input para is null! camid="+camid+",cmdParaMap="+cmdParaMap);
			return;
		}
		HuaXiaRmRspMsgCache rspmsgcache = this.camidRspMsgMap.get(camid);
		if(null == rspmsgcache){
			rspmsgcache = new HuaXiaRmRspMsgCache();
			this.camidRspMsgMap.put(camid, rspmsgcache);
		}
		synchronized (rspmsgcache){
			HuaXiaRmRspTrigerData triger_data = rspmsgcache.getTriger_data();
			if(null == triger_data){
				triger_data = new HuaXiaRmRspTrigerData();
				rspmsgcache.setTriger_data(triger_data);
			}
	        for (Map.Entry<String, Object> entry : cmdParaMap.entrySet()) {        	
	        	String fldval = (String)entry.getValue();
	        	triger_data.setAction(fldval);
	        	logger.info("cacheHuaXiaCmd_Triger_Data add cache ok! triger_data:"+triger_data.toString());
	        }	
		}
	}

	@Override
	public void cacheHuaXiaCmd_Whitelist_Data(String camid, Map<String, Object> cmdParaMap) {
		if(null == camid || camid.equals("") || null == cmdParaMap || 0 == cmdParaMap.size()){
			logger.error("cacheHuaXiaCmd_Whitelist_Data input para is null! camid="+camid+",cmdParaMap="+cmdParaMap);
			return;
		}
		HuaXiaRmRspMsgCache rspmsgcache = this.camidRspMsgMap.get(camid);
		if(null == rspmsgcache){
			rspmsgcache = new HuaXiaRmRspMsgCache();
			this.camidRspMsgMap.put(camid, rspmsgcache);
		}
		synchronized (rspmsgcache){
			HuaXiaRmRspWhiteNameList whitelist_data = rspmsgcache.getWhitelist_data();
			if(null == whitelist_data){
				whitelist_data = new HuaXiaRmRspWhiteNameList();
				rspmsgcache.setWhitelist_data(whitelist_data);
			}
			String Action = (String)cmdParaMap.get("Action");
			String PlateNumber = (String)cmdParaMap.get("PlateNumber");
			String Type = (String)cmdParaMap.get("Type");
			String Start = (String)cmdParaMap.get("Start");
			String End = (String)cmdParaMap.get("End");
			String[] PlateNumArr = PlateNumber.trim().split(",");
			for(String plateNum : PlateNumArr){
				plateNum = plateNum.trim();
				if(plateNum != null && !plateNum.equals("")){
					Map<String, String> tmpMap = new HashMap<>();
					tmpMap.put("Action", Action);
					tmpMap.put("PlateNumber", plateNum);
					tmpMap.put("Type", Type);
					tmpMap.put("Start", Start);
					tmpMap.put("End", End);
			        if(tmpMap.size() > 0){
			        	whitelist_data.addWhiteName(tmpMap);
			        	logger.info("cacheHuaXiaCmd_Whitelist_Data add cache ok, whitename:"+tmpMap.toString());
			        }
				}
			}
		}
		
	}

	@Override
	public HuaXiaRmRspMsg getHuaXiaRspMsg(String camid, String msgtype) {
		if(null == camid || camid.equals("")){
			logger.error("getHuaXiaRspMsg, input para is null! camid="+camid);
			return null;
		}
		HuaXiaRmRspMsgCache rspmsgcache = this.camidRspMsgMap.get(camid);
		if(null == rspmsgcache){
			logger.info("getHuaXiaRspMsg, no response message to send!");
			return null;
		}	
		synchronized (rspmsgcache){
			HuaXiaRmRspMsg rspmsg = new HuaXiaRmRspMsg();
			HuaXiaRmRspGpioData gpio_data_cache = rspmsgcache.getGpio_data();
			HuaXiaRmRspRs485Data rs485_data_cache = rspmsgcache.getRs485_data();
			HuaXiaRmRspTrigerData triger_data_cache = rspmsgcache.getTriger_data();
			HuaXiaRmRspWhiteNameList whitelist_data_cache = rspmsgcache.getWhitelist_data();
			if(null != gpio_data_cache && 0 != gpio_data_cache.getGpio_data().size()){
				List<Map<String, String>> gpio_data = new ArrayList<Map<String, String>>();
				rspmsg.setGpio_data(gpio_data);
				for(int i=0; i<gpio_data_cache.getGpio_data().size(); i++){
					gpio_data.add(gpio_data_cache.getGpioDataElement(i));
				}
     			gpio_data_cache.clearGpioDataElement();
     			rspmsgcache.setGpio_data(null);
			}
			if(null != rs485_data_cache && 0 != rs485_data_cache.getRs485_data().size()){
				List<Map<String, String>> rs485_data = new ArrayList<Map<String, String>>();
				rspmsg.setRs485_data(rs485_data);
				for(int i=0; i<rs485_data_cache.getRs485_data().size(); i++){
					rs485_data.add(rs485_data_cache.getRs485DataElement(i));
				}
				rs485_data_cache.clearRs485DataElement();
				rspmsgcache.setRs485_data(null);
			}		
			if(null != triger_data_cache){
				HuaXiaRmRspTrigerData triger_data = new HuaXiaRmRspTrigerData();
				rspmsg.setTriger_data(triger_data);
				triger_data.setAction(triger_data_cache.getAction());				
				rspmsgcache.setTriger_data(null);
			}
			if(msgtype.equals("heartbeat")){
				if(null != whitelist_data_cache && 0 != whitelist_data_cache.getWhitelist_data().size()){
					List<Map<String, String>> whitelist_data = new ArrayList<Map<String, String>>();
					rspmsg.setWhitelist_data(whitelist_data);
					for(int i=0; i<whitelist_data_cache.getWhitelist_data().size(); i++){
						whitelist_data.add(whitelist_data_cache.getWhiteName(i));
					}
					whitelist_data_cache.clearWhiteList();
					rspmsgcache.setWhitelist_data(null);
				}
			}
			logger.info("getHuaXiaRspMsg, get response message successfully,rspmsg"+rspmsg);
			return rspmsg;
		} 	
	}

}
